package com.joycessh.web.controller;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.joycessh.bean.EmpAchive;
import com.joycessh.bean.EmpPosition;
import com.joycessh.bean.Employee;
import com.joycessh.dao.ArchiveRegister;
import com.joycessh.dao.impl.ArchiveRegisterImpl;

/**
 * Servlet implementation class ArchiveRegisterServlet
 */
@WebServlet("/ArchiveRegisterServlet")
public class ArchiveRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @author joycessh
     * @see HttpServlet#HttpServlet()
     */
    public ArchiveRegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		request.setCharacterEncoding("utf8");
		
		//private int id;
		String empName = request.getParameter("empName");
		String sex = request.getParameter("sex");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String qq = request.getParameter("qq");
		String cellPhone = request.getParameter("cellPhone");
		String address = request.getParameter("address");
		String post = request.getParameter("post");
		String country = request.getParameter("country");
		String birthPlace = request.getParameter("birthPlace");
		String birthday = request.getParameter("birthday");
		String nation = request.getParameter("nation");
		String religion = request.getParameter("religion");
		String politicalVisage = request.getParameter("politicalVisage");
		String idNumber = request.getParameter("idNumber");
		String ssn = request.getParameter("ssn");
		String age = request.getParameter("age");
		String education = request.getParameter("education");
		String educationYears = request.getParameter("educationYears");
		String major = request.getParameter("major");
		String salaryStandardId = request.getParameter("salaryStandardId");
		String bank = request.getParameter("bank");
		String bankAccount = request.getParameter("bankAccount");
		String specialty = request.getParameter("specialty");
		String hobby = request.getParameter("hobby");
		String resume = request.getParameter("resume");
		String family = request.getParameter("family");
		String remarks = request.getParameter("remarks");
		
		//Date date = new Date(birthday);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		try {
			date = sdf.parse(birthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 java.sql.Date sqlDate = new  java.sql.Date(date.getTime()); 
		
		
		//Date date = new Date(); 
		int i = Integer.parseInt(salaryStandardId); 
		
		Employee employee = new Employee();
		employee.setEmpName(empName);
		employee.setSex(sex);
		employee.setEmail(email);
		employee.setPhone(phone);
		employee.setQq(qq);
		employee.setCellPhone(cellPhone);
		employee.setAddress(address);
		employee.setPost(post);
		employee.setCountry(country);
		employee.setBirthPlace(birthPlace);
		employee.setBirthday(sqlDate);
		employee.setNation(nation);
		employee.setReligion(religion);
		employee.setPoliticalVisage(politicalVisage);
		employee.setIdNumber(idNumber);
		employee.setSsn(ssn);
		employee.setAge(age);
		employee.setEducation(education);
		employee.setEducationYears(educationYears);
		employee.setMajor(major);
		employee.setSalaryStandardId(i);
		employee.setBank(bank);
		employee.setBankAccount(bankAccount);
		employee.setSpecialty(specialty);
		employee.setHobby(hobby);
		employee.setResume(resume);
		employee.setFamily(family);
		employee.setRemarks(remarks);
		
		int empId = 0;
		String orgIName = request.getParameter("orgIName");
		String orgIiName = request.getParameter("orgIiName");
		String orgIiiName = request.getParameter("orgIiiName");
		String positionTypeName = request.getParameter("positionTypeName");
		String positionName = request.getParameter("positionName");
		
		EmpPosition empPosition = new EmpPosition();
		empPosition.setOrgIName(orgIName);
		empPosition.setOrgIiName(orgIiName);
		empPosition.setOrgIiiName(orgIiiName);
		empPosition.setPositionTypeName(positionTypeName);
		empPosition.setPositionName(positionName);
		
		String id = null;
		//int empId;
		String booker = request.getParameter("booker");
		Timestamp bookTime = new Timestamp(System.currentTimeMillis());
		//String checker = request.getParameter("checker");
		//Timestamp checkTime;
		//String stateTypeId = request.getParameter("stateTypeId");
		
		//int state = Integer.parseInt(stateTypeId);
		
		EmpAchive empAchive = new EmpAchive();
		//empAchive.setId(id);
		//empAchive.setEmpId(empId);
		empAchive.setBooker(booker);
		empAchive.setBookTime(bookTime);
		empAchive.setStateTypeId(1);
		
		ArchiveRegister archiveReg = new ArchiveRegisterImpl();
		String forward = "";
		
		try {
			if (archiveReg.insertEmployee(employee)) {
					empId = employee.getId();
					empPosition.setEmpId(empId);
					empAchive.setEmpId(empId);
					String str = Integer.toString(empId);
					id = "20171211"+str;
					empAchive.setId(id);		
					
					archiveReg.insertEmpPosition(empPosition);
					archiveReg.insertEmpAchive(empAchive);
					forward = "success.jsp";					
					
				} else {
					forward = "input.jsp";
				}
				
			RequestDispatcher rd = request.getRequestDispatcher(forward);
			rd.forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
